package zx.x.modules.app.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import zx.x.modules.app.dto.OrderDto;
import zx.x.modules.app.entity.OrderEntity;
import zx.x.modules.app.entity.SysOrderLogEntity;
import zx.x.modules.app.entity.SysOrderTaskEntity;
import zx.x.modules.app.service.*;
import zx.x.modules.app.utils.BeanUtils;
import zx.x.modules.common.entity.CreativeGoodsEntity;
import zx.x.modules.common.service.CreativeGoodsService;

import java.util.TimerTask;

public class OrderTask extends TimerTask {
    OrderDto  o;
    public OrderTask(OrderDto  order) {
        this.o = order;
    }

    // 特殊注入
    OrderService orderService = (OrderService) ApplicationContextUtil.getBean("orderService");
    RoomAppointmentService roomAppointmentService = (RoomAppointmentService) ApplicationContextUtil.getBean("roomAppointmentService");
    SysOrderLogService sysOrderLogService = (SysOrderLogService) ApplicationContextUtil.getBean("sysOrderLogService");
    SysOrderTaskService sysOrderTaskService = (SysOrderTaskService) ApplicationContextUtil.getBean("sysOrderTaskService");
    CreativeGoodsService creativeGoodsService = (CreativeGoodsService) ApplicationContextUtil.getBean("creativeGoodsService");
    DybService dybService = (DybService) ApplicationContextUtil.getBean("dybService");


    public void run() {
        if (o.getType() == 1 || o.getType() == 2){
            QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<OrderEntity>();
            queryWrapper.eq("id", o.getId()).in("zt", "1","6").eq("user_id", o.getUserId()).isNull("tkzt");
            // 修改定时任务log表
            QueryWrapper<SysOrderTaskEntity> orderTaskQueryWrapper = new QueryWrapper<SysOrderTaskEntity>();
            orderTaskQueryWrapper.eq("o_id",o.getId());
            SysOrderTaskEntity taskObj = new SysOrderTaskEntity();
            taskObj.setZt(1);
            // 订单已付款就不用后续操作
            if (orderService.getOne(queryWrapper) == null){
                // 取消订单
                orderService.updateById(new OrderEntity(){{setId(o.getId());setZt(2L);}}); //2是取消订单

                // 添加订单日志表
                sysOrderLogService.save(new SysOrderLogEntity(){{setZt(0);setLogText("订单超时,系统自动取消");setOId(o.getId());}});// 0取消付款

                // 取消预约表
                String[] ids = o.getRoomIds().split(",");
                for (String id: ids) {
                    OrderDto obj = new OrderDto();
                    BeanUtils.copyProperties(o, obj);
                    obj.setZt(1L);// 1是取消预约
                    obj.setRoomId(id);
                    roomAppointmentService.cancelReservation(obj);
                }
                System.out.println("这里可以触发你要触发的事件！");
                taskObj.setText("未付款,自动取消订单");
            }else{
                taskObj.setText("付款成功");
            }
            sysOrderTaskService.update(taskObj,orderTaskQueryWrapper);
        }else if (o.getType() == 3){
            QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<OrderEntity>();
            queryWrapper.eq("id", o.getId()).eq("zt", "0").eq("user_id", o.getUserId());
            // 修改定时任务log表
            QueryWrapper<SysOrderTaskEntity> orderTaskQueryWrapper = new QueryWrapper<SysOrderTaskEntity>();
            orderTaskQueryWrapper.eq("o_id",o.getId());
            SysOrderTaskEntity taskObj = new SysOrderTaskEntity();
            taskObj.setZt(1);
            // 订单已付款就不用后续操作
            if (orderService.getOne(queryWrapper) != null){
                // 取消订单
                orderService.updateById(new OrderEntity(){{setId(o.getId());setZt(2L);}}); //2是取消订单
                // 添加订单日志表
                sysOrderLogService.save(new SysOrderLogEntity(){{setZt(0);setLogText("订单超时,系统自动取消");setOId(o.getId());}});// 0取消付款

                // 获取下最新版本号
                CreativeGoodsEntity c = creativeGoodsService.getById(o.getSpId());
                // 修改数量
                QueryWrapper<CreativeGoodsEntity> querySpWrapper = new QueryWrapper<CreativeGoodsEntity>();
                querySpWrapper.eq("id", o.getSpId()).eq("version",c.getVersion());
                c.setSl(c.getSl()+o.getSl());
                c.setVersion(c.getVersion()+1);
                creativeGoodsService.update(c,querySpWrapper);
                taskObj.setText("未付款,自动取消订单");
            }else{
                taskObj.setText("付款成功");
            }
            sysOrderTaskService.update(taskObj,orderTaskQueryWrapper);
        } else if (o.getType() == 5){
            QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<OrderEntity>();
            queryWrapper.eq("id", o.getId()).eq("zt", "0").eq("user_id", o.getUserId());
            // 修改定时任务log表
            QueryWrapper<SysOrderTaskEntity> orderTaskQueryWrapper = new QueryWrapper<SysOrderTaskEntity>();
            orderTaskQueryWrapper.eq("o_id",o.getId());
            SysOrderTaskEntity taskObj = new SysOrderTaskEntity();
            taskObj.setZt(1);
            // 订单已付款就不用后续操作
            if (orderService.getOne(queryWrapper) != null){
                // 取消订单
                orderService.updateById(new OrderEntity(){{setId(o.getId());setZt(2L);}}); //2是取消订单
                // 添加订单日志表
                sysOrderLogService.save(new SysOrderLogEntity(){{setZt(0);setLogText("订单超时,系统自动取消");setOId(o.getId());}});// 0取消付款
                taskObj.setText("未付款,自动取消订单");
            }else{
                taskObj.setText("付款成功");
            }
            sysOrderTaskService.update(taskObj,orderTaskQueryWrapper);
        }else if (o.getType() == 6){
            QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<OrderEntity>();
            queryWrapper.eq("id", o.getId()).eq("zt", "0").eq("user_id", o.getUserId());
            // 修改定时任务log表
            QueryWrapper<SysOrderTaskEntity> orderTaskQueryWrapper = new QueryWrapper<SysOrderTaskEntity>();
            orderTaskQueryWrapper.eq("o_id",o.getId());
            SysOrderTaskEntity taskObj = new SysOrderTaskEntity();
            taskObj.setZt(1);
            // 订单已付款就不用后续操作
            if (orderService.getOne(queryWrapper) != null){
                // 取消订单
                orderService.updateById(new OrderEntity(){{setId(o.getId());setZt(2L);}}); //2是取消订单
                // 添加订单日志表
                sysOrderLogService.save(new SysOrderLogEntity(){{setZt(0);setLogText("订单超时,系统自动取消");setOId(o.getId());}});// 0取消付款
                taskObj.setText("未付款,自动取消订单");
            }else{
                taskObj.setText("付款成功");
            }
            sysOrderTaskService.update(taskObj,orderTaskQueryWrapper);
        }
        System.gc();//关闭线程
        cancel();//关闭线程
    }
}
